package org.saratogaschools.edd.firefighter.client.activity;

import java.util.List;

import org.saratogaschools.edd.firefighter.client.ClientFactory;
import org.saratogaschools.edd.firefighter.client.place.AddFirePlace;
import org.saratogaschools.edd.firefighter.client.place.FireListPlace;
import org.saratogaschools.edd.firefighter.client.place.FirePlace;
import org.saratogaschools.edd.firefighter.client.util.AsyncPlaceChange;
import org.saratogaschools.edd.firefighter.client.view.FireListView;
import org.saratogaschools.edd.firefighter.shared.model.Fire;
import org.saratogaschools.edd.firefighter.shared.model.FireFighter;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class FireListActivity extends AbstractActivity implements FireListView.Presenter, AsyncCallback<List<Fire>> {
	
	private ClientFactory clientFactory;
	private FireFighter fighter;
	private FireListView view;

	public FireListActivity(FireListPlace place, ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		this.fighter = place.getFighter();
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		this.view = new FireListView();
		view.setPresenter(this);
		panel.setWidget(view);
		view.setAddButtonEnabled(fighter.canManageFires());
		refresh();
	}
	
	@Override
	public void refresh() {
		clientFactory.getService().getFiresFromDepartment(fighter.getDepartmentKey(), this);
	}

	@Override
	public void onFailure(Throwable caught) {
	}

	@Override
	public void onSuccess(List<Fire> result) {
		view.setFires(result);
	}

	@Override
	public void addFire() {
		GWT.runAsync(new AsyncPlaceChange(clientFactory, new AddFirePlace(fighter)));
	}

	@Override
	public void selectFire(Fire f) {
		GWT.runAsync(new AsyncPlaceChange(clientFactory, new FirePlace(fighter, f)));
	}
	

	

}
